package fr.cephb.util;

public abstract class Cast<T>
	{
	public abstract T cast(String s);
	public boolean isA(String s)
		{
		return cast(s)!=null;
		}
	
	protected String trim(String s)
		{
		return (s==null?null:s.trim());
		}
	
	public static final Cast<Integer> Integer=new Cast<java.lang.Integer>()
		{
		@Override
		public java.lang.Integer cast(String s)
			{
			if(s==null) return null;
			try
				{
				return new java.lang.Integer(trim(s));
				}
			catch (NumberFormatException e)
				{
				return null;
				}
			}
		};
		
		
		public static final Cast<Double> Double=new Cast<java.lang.Double>()
			{
			@Override
			public java.lang.Double cast(String s)
				{
				if(s==null) return null;
				try
					{
					return new java.lang.Double(trim(s));
					}
				catch (NumberFormatException e)
					{
					return null;
					}
				}
			};	
		
		public static final Cast<Integer> IntegerGreaterThanZero=new Cast<java.lang.Integer>()
			{
			@Override
			public java.lang.Integer cast(String s)
				{
				java.lang.Integer i= Integer.cast(s);
				if(i==null || i<=0) return null;
				return i;
				}
			};
			
		public static final Cast<Integer> NonNegativeInteger=new Cast<java.lang.Integer>()
			{
			@Override
			public java.lang.Integer cast(String s)
				{
				java.lang.Integer i= Integer.cast(s);
				if(i==null || i<0) return null;
				return i;
				}
			};
	}
